<?php


namespace app\admin\validate;


use app\admin\model\ContractModel;
use app\admin\model\OperateModel;
use think\Db;
use think\Validate;

class ContractForm extends Validate
{
    protected $rule = [
        'lt_id|联通公司的套餐id' =>'number|max:11',
        'name|套餐名字' =>'require|max:60',
        'price|价格' =>'require|float|max:32',
        'operate_id|运营中心id' =>'require|number|max:11',
        'type_name|套餐级别' =>'require|in:4G,5G',
        'discount|套餐折扣比例' =>'require|max:6',
        '12_commission|担保12月佣金' =>'require|float|max:32',
        '24_commission|担保24月佣金' =>'require|float|max:32',
        'images|图片' =>'require'

    ];
    public function add($array){
        $array["operate_name"] = OperateModel::getNameById($array['operate_id']);
        $array["price"]*=100;
        if(!$array["operate_name"]){
            clientReturn("not_find",'运营中心');
        }
        $array["create_time"] =date("Y-m-d H:i:s");
        return checkRes((new ContractModel())->add($array));
    }
    public function edit($id,$array){
        $array["price"]*=100;
        if($array['operate_id']) unset($array['operate_id']);
        if($array['id']) unset($array['id']);
        return checkRes((new ContractModel())->edit($id,$array));
    }
    public static function addTemplate($array){
        $array["price"]*=100;
        $array["create_time"] =date("Y-m-d H:i:s");
        return checkRes(Db::name('contract_template')->insert($array));
    }
    public static function checkHas($id){
        return Db::name('contract')->where('template_id',$id)->value('id');
    }
    public static function editTemplate($array){
        $array["price"]*=100;
        return checkRes(Db::name('contract_template')->update($array),'修改');
    }
}